/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.da;


import com.stip.server.entities.UserHub;
import com.stip.server.entities.UsersMain;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
public class UsersHubDA {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager em;
    public UserHub finduUsersHubByUID(String uid){
    UserHub usersHub=em.find(UserHub.class, uid);
    return  usersHub;
    
    }
    public UserHub checkUserHub(String uid,String name){
    UserHub userHub=em.find(UserHub.class, uid);
        if (userHub==null) {
            return null;
        } else {
            if (userHub.getName() == null ? name == null : userHub.getName().equals(name)) {
                return  userHub;
            } else {
                return null;
            }
        }
    }
    public String createUserHub(String uid,String name,String department,String major,String classInfo){
    UserHub   userHub=new UserHub();
    userHub.setClass1(classInfo);
    userHub.setDepartment(department);
    userHub.setMajor(major);
    userHub.setUid(uid);
    userHub.setName(name);
    userHub.setUsersMain(em.find(UsersMain.class, uid));
    em.persist(userHub);
    return  userHub.getUid();
    }
    public Boolean eidtUserHub(String uid,String name,String department,String major,String classInfo){
    UserHub userHub=em.find(UserHub.class, uid);
        if (userHub!=null) {
            if (name==null) {
                userHub.setName(userHub.getName());
            } else {
                userHub.setName(name);
            }
            if (department==null) {
                userHub.setDepartment(userHub.getDepartment());
            } else {
                userHub.setDepartment(department);
            }
            if (major==null) {
                userHub.setMajor(userHub.getMajor());
            } else {
                userHub.setMajor(major);
            }
            if (classInfo==null) {
                userHub.setMajor(userHub.getMajor());
            } else {
                userHub.setMajor(major);
            }
            em.merge(userHub);
            return  true;
        } else {
            return  false;
        }
    
    } 
    public Boolean removeUserHub(String uid){
    UserHub userHub=em.find(UserHub.class, uid);
        if (userHub!=null) {
            em.remove(userHub);
            return  true;
        } else {
            return  false;
        }
    
    
    }
}
